import Taro from '@tarojs/taro'
import app from '../../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            patient: {
                id: '',
                name: '',
                head_image: '',
            },
            plan: {
                id: this.$router.params.id,
                title: '',
                date_type: '',
                start_date: '',
            }
        };
        return this.state;
    },
    getPatient(id) {
        var me = this;
        app.get('web/patient/get', {id: id}, function(res) {
            me.setState({
                patient: res.data.result
            });
        });
    },
    getPlan() {
        var me = this;
        app.get('web/flow_plan/get', {id: me.state.plan.id}, function(res) {
            var plan = res.data.result;
            var items = plan.items;
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                if (item.status == '未发送') {
                    item.status = '待发送';
                }
                item.start_time = item.start_time.split(' ')[0];
            }
            plan.items = items;
            me.setState({
                plan: plan
            });
            me.getPatient(plan.patient_id);
        });
    },
    cancelPlan() {
        var me = this;
        app.isConfirm('是否取消该计划', function(res) {
            app.post('web/flow_plan/cancel', {id: me.state.plan.id}, function(res) {
                var status = res.data.status;
                if (status == 200) {
                    app.success('取消成功', function(res) {
                        setTimeout(() => {
                            app.redirectTo('../suifangHuanzheLieBiao/suifangHuanzheLieBiao');
                        }, 1000);
                    });
                }
            });
        });
    },
    componentWillMount () {
        if(this.init) {
            this.init();
        }
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = v;
        this.setState({n: temp[names[i]]});
    },
    handleTextareaChange(name, e) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        temp[names[i]] = e.target.value;
        this.setState({n: temp[names[i]]});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
